package liumengbaio.lolzhangyoubao.Activity.utils;

import android.content.Context;

import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.WeekFree;

/**
 * 获取Assent中的数据
 * Created by lanouhn on 16/8/25.
 */
public class AssetsUtils {


    public static String getChampion_id(Context context, String id) {

        try {
            InputStream is = context.getAssets().open("hero/heroTgpIds.json");

            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();

            // Convert the buffer into a string.
            String text = new String(buffer, "UTF-8");

            JSONObject hJson = new JSONObject(text);

            Set<String> set = getAllKeys(hJson);
            for (String key : set) {
                String value = hJson.getString(key);
                if (value.equals(id))
                    return key;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }


    /**
     * 获取本期周免英雄的方法
     *
     * @param list 本周免费英雄的id
     * @return
     */
    public static List<FreeHeroInfo> getFreeHerosList(Context context, List<WeekFree.DataBean.CurrentperiodBean> list) {

        List<FreeHeroInfo> fhList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            FreeHeroInfo info = getHeroInfo(context, list.get(i).getId());

            fhList.add(info);
        }
        return fhList;
    }


    /**
     * 获取对应Id的英雄信息
     *
     * @param id
     * @return
     */
    private static FreeHeroInfo getHeroInfo(Context context, String id) {

        FreeHeroInfo info = new FreeHeroInfo();
        try {
            InputStream is = context.getAssets().open("hero/" + id + ".json");

            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();

            // Convert the buffer into a string.
            String text = new String(buffer, "UTF-8");

            JSONObject hJson = new JSONObject(text);

            info.setNickname(hJson.getString("nickname"));
            info.setPic_url(hJson.getString("pic_url"));
            info.setMoney(hJson.getString("money"));
            info.setPoint(hJson.getString("point"));
            info.setName(hJson.getString("name"));
            info.setFilter(hJson.getString("filter"));
            info.setId(hJson.getString("id"));
            info.setBase_physical(hJson.getString("base_physical"));
            info.setBase_magic(hJson.getString("base_magic"));
            info.setBase_blood(hJson.getString("base_blood"));
            info.setBase_difficulty(hJson.getString("base_difficulty"));
            info.setMoving_speed(hJson.getString("moving_speed"));
            info.setRange(hJson.getString("range"));
            info.setAttack(hJson.getString("attack"));
            info.setAttack_speed(hJson.getString("attack_speed"));
            info.setArmor(hJson.getString("armor"));
            info.setBlood(hJson.getString("blood"));
            info.setMana(hJson.getString("mana"));
            info.setMagic_resistance(hJson.getString("magic_resistance"));
            info.setBlood_recovery(hJson.getString("blood_recovery"));
            info.setMagic_recovery(hJson.getString("magic_recovery"));
            info.setUsetip(hJson.getString("usetip"));
            info.setKilltip(hJson.getString("killtip"));
            info.setBg(hJson.getString("bg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }


    /**
     * 得到JSONObject里的所有key
     *
     * @param jsonObject JSONObject实例对象
     * @return Set
     */
    private static Set<String> getAllKeys(JSONObject jsonObject) {
        return getAllKeys(jsonObject.toString());
    }

    /**
     * 从JSON字符串里得到所有key
     *
     * @param jsonString json字符串
     * @return Set
     */
    private static Set<String> getAllKeys(String jsonString) {
        Set<String> set = new HashSet<>();
        //按照","将json字符串分割成String数组
        String[] keyValue = jsonString.split(",");
        for (int i = 0; i < keyValue.length; i++) {
            String s = keyValue[i];
            //找到":"所在的位置，然后截取
            int index = s.indexOf(":");
            //第一个字符串因带有json的"{"，需要特殊处理
            if (i == 0) {
                set.add(s.substring(2, index - 1));
            } else {
                set.add(s.substring(1, index - 1));
            }
        }
        return set;
    }


}
